<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        html,
        body {
            margin: 0;
            padding: 0;
        }
        
        #fileInput {
            margin: 12px, 0;
        }
    </style>
</head>

<body>
    <div class="fileInput">
        <input type="file" accept="image/*" id="fileInput" />
    </div>
    <canvas id="canvas" height="600" width="600"></canvas>

    <script>
        const canvas = document.getElementById("canvas");
        const ctx = canvas.getContext("2d");
        const fileInput = document.querySelector("#fileInput");
        const img = new Image();
        fileInput.addEventListener("change", handleFileInputChange, false);

        function handleFileInputChange(e) {
            const file = e.target.files[0];
            const reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = (e) => {
                let result = e.target.result;
                img.src = result;
                img.onload = function() {
                    const {
                        width,
                        height
                    } = this;
                    ctx.fillStyle = `rgba(0,0,0,0.5)`;
                    ctx.fillRect(160, 190, width, height);

                    ctx.globalCompositeOperation = "destination-out";

                    ctx.fillStyle = "#000";
                    ctx.fillRect(270, 300, 50, -50);
                    ctx.globalCompositeOperation = "destination-over";
                    ctx.drawImage(img, 160, 190);
                };
            };
        }
    </script>
</body>

</html>